package boogui.member;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.DropMode;
import javax.swing.event.ChangeEvent;
import javax.swing.text.ChangedCharSetException;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class S_MemberInfoModify extends JDialog {
		
	private JTextField nameTextField;
	private JTextField hpTextField_1;
	private JTextField emailTextField;
	private JPasswordField pwdField;
	private JTextField hptextField_2;
	private JComboBox hpCbx;
	private JComboBox emailCbx;
	private JComboBox yearCbx;
	private JComboBox monthCbx;
	private JComboBox dayCbx;
	private JComboBox genreCbx;
	private boolean isId = false; ///////////////회원정보 수정
	private boolean isHp = false; 
	private boolean isEm = false; 
	private MemberVO m = null;/////////////////////////////////////////회원정보수정
	private JButton hpBtn = null;
	public S_MemberInfoModify(JFrame p) {
		super(p);
		
		m = new MemberVO();
		hpBtn = new JButton();
		IMemberLoginService ms = new MemberLoginService();
		ms.getloginView(m);			
		
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				S_MemberInfoModify.class.getResource("/images/user.png")));
		setModalityType(ModalityType.DOCUMENT_MODAL);
		setTitle("회원 정보 수정");
		setSize(380, 480);
		setLocationRelativeTo(null);
		setResizable(false);

		getContentPane().setLayout(new BorderLayout());
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setPreferredSize(new Dimension(10, 50));
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("정보수정");///////////////////////////////////////회원정보수정
				okButton.addActionListener(new BtnActionListener());
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("취소");
				cancelButton.addActionListener(new BtnActionListener());
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
			{
				JPanel panel_1 = new JPanel();
				panel.add(panel_1);
				GridBagLayout gbl_panel_1 = new GridBagLayout();
				gbl_panel_1.columnWidths = new int[] { 10, 0, 0, 0, 41, 0, 0,
						0, 0 };
				gbl_panel_1.rowHeights = new int[] { 35, 0, 20, 0, 20, 0, 20,
						0, 20, 0, 20, 20, 0, 20, 0, 0 };
				gbl_panel_1.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
						0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
				gbl_panel_1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
						0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
						Double.MIN_VALUE };
				panel_1.setLayout(gbl_panel_1);
				{
					JLabel lblNewLabel = new JLabel("아   이   디");
					GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
					gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel.gridx = 1;
					gbc_lblNewLabel.gridy = 1;
					panel_1.add(lblNewLabel, gbc_lblNewLabel);
				}
				{						
					
					JLabel lblNewLabel_7 = new JLabel(m.getMemid());///////////////////////////////////////회원정보수정
					GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints();
					gbc_lblNewLabel_7.gridwidth = 3;
					gbc_lblNewLabel_7.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_7.gridx = 3;
					gbc_lblNewLabel_7.gridy = 1;
					panel_1.add(lblNewLabel_7, gbc_lblNewLabel_7);
				}
				{
					JLabel lblNewLabel_1 = new JLabel("비 밀 번 호");
					GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
					gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_1.gridx = 1;
					gbc_lblNewLabel_1.gridy = 3;
					panel_1.add(lblNewLabel_1, gbc_lblNewLabel_1);
				}
				{
					pwdField = new JPasswordField();
					pwdField.setToolTipText("비밀번호를 입력하세요");
					GridBagConstraints gbc_pwdField = new GridBagConstraints();
					gbc_pwdField.gridwidth = 3;
					gbc_pwdField.insets = new Insets(0, 0, 5, 5);
					gbc_pwdField.fill = GridBagConstraints.HORIZONTAL;
					gbc_pwdField.gridx = 3;
					gbc_pwdField.gridy = 3;
					panel_1.add(pwdField, gbc_pwdField);
				}
				{
					JLabel lblNewLabel_2 = new JLabel("이         름");
					GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
					gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_2.gridx = 1;
					gbc_lblNewLabel_2.gridy = 5;
					panel_1.add(lblNewLabel_2, gbc_lblNewLabel_2);
				}
				{
					nameTextField = new JTextField(m.getMemname());
					nameTextField.setText(m.getMemname());
					nameTextField.setToolTipText("이름을 입력하세요");
					GridBagConstraints gbc_nameTextField = new GridBagConstraints();
					gbc_nameTextField.gridwidth = 3;
					gbc_nameTextField.fill = GridBagConstraints.HORIZONTAL;
					gbc_nameTextField.insets = new Insets(0, 0, 5, 5);
					gbc_nameTextField.gridx = 3;
					gbc_nameTextField.gridy = 5;
					panel_1.add(nameTextField, gbc_nameTextField);
					nameTextField.setMargin(new Insets(3, 3, 3, 3));
					nameTextField.setColumns(10);
				}
				
				
				{
					JLabel lblNewLabel_3 = new JLabel("핸   드   폰");
					GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
					gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_3.gridx = 1;
					gbc_lblNewLabel_3.gridy = 7;
					panel_1.add(lblNewLabel_3, gbc_lblNewLabel_3);				
					
				}
				{
					String[] hp = { "010", "011", "016", "017", "018", "019" };
					hpCbx = new JComboBox(hp);
					GridBagConstraints gbc_hpCbx = new GridBagConstraints();
					gbc_hpCbx.fill = GridBagConstraints.HORIZONTAL;
					gbc_hpCbx.insets = new Insets(0, 0, 5, 5);
					gbc_hpCbx.gridx = 3;
					gbc_hpCbx.gridy = 7;
					panel_1.add(hpCbx, gbc_hpCbx);
					
				}
				{
					String hp1= m.getMemhp().substring(3,7);////////////////////////////////////
					String hp2 = m.getMemhp().substring(7,11);///////////////////////////////////
					
					hpTextField_1 = new JTextField(hp1);
					
					
						
					
					GridBagConstraints gbc_hpTextField_1 = new GridBagConstraints();
					gbc_hpTextField_1.fill = GridBagConstraints.HORIZONTAL;
					gbc_hpTextField_1.insets = new Insets(0, 0, 5, 5);
					gbc_hpTextField_1.gridx = 4;
					gbc_hpTextField_1.gridy = 7;
					panel_1.add(hpTextField_1, gbc_hpTextField_1);
					hpTextField_1.setMargin(new Insets(3, 3, 3, 3));
					hpTextField_1.setColumns(10);
					
					hptextField_2 = new JTextField(hp2);
					hptextField_2.addKeyListener(new KeyAdapter() {
						@Override
						public void keyPressed(KeyEvent e) {
							
						}
					});
					GridBagConstraints gbc_hptextField_2 = new GridBagConstraints();
					gbc_hptextField_2.insets = new Insets(0, 0, 5, 5);
					gbc_hptextField_2.fill = GridBagConstraints.HORIZONTAL;
					gbc_hptextField_2.gridx = 5;
					gbc_hptextField_2.gridy = 7;
					panel_1.add(hptextField_2, gbc_hptextField_2);
					hptextField_2.setColumns(10);
					
					
					
				}
				{
					
					final JButton hpBtn = new JButton("핸드폰 확인");
					GridBagConstraints gbc_hpBtn = new GridBagConstraints();
					hpBtn.addActionListener(new BtnActionListener());{}
					hpBtn.setActionCommand("hpchk");
					gbc_hpBtn.anchor = GridBagConstraints.WEST;
					gbc_hpBtn.insets = new Insets(0, 0, 5, 5);
					gbc_hpBtn.gridx = 6;
					gbc_hpBtn.gridy = 7;
					panel_1.add(hpBtn, gbc_hpBtn);
					
					String hp = (String) hpCbx.getSelectedItem();
					String hp2 = hpTextField_1.getText();
					String hp3 = hptextField_2.getText();
					System.out.println(m.getMemhp());
					System.out.println(hp+hp2+hp3);
					if(m.getMemhp().equals(hp+hp2+hp3)){
						System.out.println(m.getMemhp());
						hpBtn.setEnabled(false);
					}else{
						hpBtn.setEnabled(true);
					}
					
					hpTextField_1.addKeyListener(new KeyAdapter() {
						@Override
						public void keyPressed(KeyEvent e) {
							
							System.out.println(hpTextField_1.getText());
							System.out.println(m.getMemhp().substring(3,7));
							
							
							if(hpTextField_1.getText().equals(m.getMemhp().substring(3,7)))
							{hpBtn.setEnabled(false);
							}else{
								hpBtn.setEnabled(true);
								
							}
							System.out.println(hpTextField_1.getText());
							System.out.println(m.getMemhp().substring(3,7));
							if(hpTextField_1.getText().equals(m.getMemhp().substring(3,7)))
							{hpBtn.setEnabled(false);
							}else{
								hpBtn.setEnabled(true);
								
							}
						}
					});
					
					

				}
				{
					JLabel lblNewLabel_4 = new JLabel("이   메   일");
					GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
					gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_4.gridx = 1;
					gbc_lblNewLabel_4.gridy = 9;
					panel_1.add(lblNewLabel_4, gbc_lblNewLabel_4);
				}
				{
					
					
					emailTextField = new JTextField();
					
					emailTextField.setToolTipText("이메일 주소를 입력하세요");
					GridBagConstraints gbc_emailTextField = new GridBagConstraints();
					gbc_emailTextField.gridwidth = 3;
					gbc_emailTextField.fill = GridBagConstraints.HORIZONTAL;
					gbc_emailTextField.insets = new Insets(0, 0, 5, 5);
					gbc_emailTextField.gridx = 3;
					gbc_emailTextField.gridy = 9;
					panel_1.add(emailTextField, gbc_emailTextField);
					emailTextField.setMargin(new Insets(3, 3, 3, 3));
					emailTextField.setColumns(10);
				}
				String[] email = { "직접입력", "naver.com", "hanmail.net",
						"gmail.com", "nate.com" };
				emailCbx = new JComboBox(email);
				GridBagConstraints gbc_emailCbx = new GridBagConstraints();
				gbc_emailCbx.anchor = GridBagConstraints.WEST;
				gbc_emailCbx.insets = new Insets(0, 0, 5, 5);
				gbc_emailCbx.gridx = 6;
				gbc_emailCbx.gridy = 9;
				panel_1.add(emailCbx, gbc_emailCbx);
				
				String email1 = "";				
				int idx = emailCbx.getSelectedIndex();
				
				
				if (idx==0) {
						email1 = m.getMememail();
				} else {
						email1= m.getMememail().substring(0,m.getMememail().indexOf('@'));
				}
				emailTextField.setText(email1);
				
				
				
				{
					JButton emailBtn = new JButton("이메일 확인");
					GridBagConstraints gbc_emailBtn = new GridBagConstraints();
					emailBtn.addActionListener(new BtnActionListener());
					emailBtn.setActionCommand("emailchk");
					gbc_emailBtn.fill = GridBagConstraints.HORIZONTAL;
					gbc_emailBtn.gridwidth = 2;
					gbc_emailBtn.insets = new Insets(0, 0, 5, 5);
					gbc_emailBtn.gridx = 5;
					gbc_emailBtn.gridy = 10;
					panel_1.add(emailBtn, gbc_emailBtn);
					
					/*String myemail1 = "untiring@naver.com";
					String myemail2 = "untiring@hanamail.com";
					String myemail3 = "untiring@gmail.com";
					String myemail4 = "untiring@nate.com";
					String myemail5 = "untiring@hotmail.com";
					System.out.println(myemail4.indexOf('@'));
					System.out.println(myemail1.substring(0,myemail1.indexOf('@')));*/
					int result = 0;
					String ema ="";
					if(m.getMememail().indexOf("naver.com")!=-1){
						ema = m.getMememail().substring(0,m.getMememail().indexOf('@'));
						result = 1;
					}else if(m.getMememail().indexOf("hanmail.com")!=-1){
						ema = m.getMememail().substring(0,m.getMememail().indexOf('@'));
						 result = 2;
					}else if(m.getMememail().indexOf("gmail.com")!=-1){
						ema = m.getMememail().substring(0,m.getMememail().indexOf('@'));
						 result = 3;
					}else if(m.getMememail().indexOf("nate.com")!=-1){
						ema = m.getMememail().substring(0,m.getMememail().indexOf('@'));
						 result = 4;
					}else	{
						ema = m.getMememail();
						 result = 0;
					}
					emailTextField.setText(ema);
					emailCbx.setSelectedIndex(result);
					}
					
				{
					JLabel lblNewLabel_5 = new JLabel("생 년 월 일");
					GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
					gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_5.gridx = 1;
					gbc_lblNewLabel_5.gridy = 12;
					panel_1.add(lblNewLabel_5, gbc_lblNewLabel_5);
				}

				String[] year = new String[100];
				for (int i = 0; i < year.length; ++i) {
					year[i] = (2013 - i) + "년";
				}
				yearCbx = new JComboBox(year);
				GridBagConstraints gbc_yearCbx = new GridBagConstraints();
				gbc_yearCbx.fill = GridBagConstraints.HORIZONTAL;
				gbc_yearCbx.insets = new Insets(0, 0, 5, 5);
				gbc_yearCbx.gridx = 3;
				gbc_yearCbx.gridy = 12;
				panel_1.add(yearCbx, gbc_yearCbx);
				
				String k = m.getMembirth().substring(0, 4)+"년";
				System.out.println(k);
				yearCbx.setSelectedItem(k);

				String[] month = new String[12];
				for (int i = 0; i < 9; ++i) {
					month[i] = "0" + (i + 1) + "월";
				}
				for (int i = 9; i < month.length; ++i) {
					month[i] = (i + 1) + "월";
				}
				monthCbx = new JComboBox(month);
				GridBagConstraints gbc_monthCbx = new GridBagConstraints();
				gbc_monthCbx.fill = GridBagConstraints.HORIZONTAL;
				gbc_monthCbx.insets = new Insets(0, 0, 5, 5);
				gbc_monthCbx.gridx = 4;
				gbc_monthCbx.gridy = 12;
				panel_1.add(monthCbx, gbc_monthCbx);
				System.out.println(m.getMembirth());
				String k1 = m.getMembirth().substring(5, 7)+"월";
				System.out.println(k1);
				monthCbx.setSelectedItem(k1);

				String[] day = new String[31];
				for (int i = 0; i < 9; ++i) {
					day[i] = "0" + (i + 1) + "일";
				}
				for (int i = 9; i < day.length; ++i) {
					day[i] = (i + 1) + "일";
				}
				
				
				
				
				dayCbx = new JComboBox(day);
				GridBagConstraints gbc_dayCbx = new GridBagConstraints();
				gbc_dayCbx.anchor = GridBagConstraints.WEST;
				gbc_dayCbx.insets = new Insets(0, 0, 5, 5);
				gbc_dayCbx.gridx = 5;
				gbc_dayCbx.gridy = 12;
				panel_1.add(dayCbx, gbc_dayCbx);
				String k2 = m.getMembirth().substring(8, 10)+"일";
				dayCbx.setSelectedItem(k2);
				{
					JLabel lblNewLabel_6 = new JLabel("선 호 장 르");
					GridBagConstraints gbc_lblNewLabel_6 = new GridBagConstraints();
					gbc_lblNewLabel_6.insets = new Insets(0, 0, 0, 5);
					gbc_lblNewLabel_6.gridx = 1;
					gbc_lblNewLabel_6.gridy = 14;
					panel_1.add(lblNewLabel_6, gbc_lblNewLabel_6);
				}
				String[] g = { "액션", "드라마", "멜로/로맨스", "코미디", "스릴러", "공포" };
				genreCbx = new JComboBox(g);
				GridBagConstraints gbc_genreCbx = new GridBagConstraints();
				gbc_genreCbx.fill = GridBagConstraints.HORIZONTAL;
				gbc_genreCbx.gridwidth = 3;
				gbc_genreCbx.insets = new Insets(0, 0, 0, 5);
				gbc_genreCbx.gridx = 3;
				gbc_genreCbx.gridy = 14;
				panel_1.add(genreCbx, gbc_genreCbx);
				int result = 0;
				switch(m.getMemgenre()){
				case "액션" : result = 0; break;
				case "드라마" : result = 1;  break;
				case "멜로/로멘스" : result = 2;  break;
				case "코미디" : result = 3;  break;
				case "스릴러" : result = 4; break;
				default : result = 5; break;
				}
				genreCbx.setSelectedIndex(result);
			}
		}

		setVisible(true);
	}

	private class BtnActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			MemberChkService chks = new MemberChkService();
			String msg = "";
			String hp = "";
			String email1 = "";
			MemberService ms = new MemberService();
			MemberVO m = new MemberVO();
			
			switch (e.getActionCommand()) {
			/*case "idchk": // 아이디 검사                           ///////////////////////////////////////////////////////회원정보수정
				m.setMemid(idTextField.getText());
				switch (chks.dupChkId(m)) {
				case 1:
					msg = "중복된 아이디가 있습니다.";
					idTextField.setText(null);
					idTextField.requestFocus();
					break;
				case 2:
					msg = "아이디를 입력하세요.";
					idTextField.requestFocus();
					break;
				case 3:
					msg = "4자 이상, 12자 이하로 입력하세요";
					idTextField.requestFocus();
					break;
				default:
					msg = "사용가능한 아이디 입니다.";
					pwdField.requestFocus();
					break;
				}
				JOptionPane.showMessageDialog(null, msg);
				break;
			*/
			
			case "hpchk": // 핸드폰 검사
				hp = (String) hpCbx.getSelectedItem();
				String hp2 = hpTextField_1.getText();
				String hp3 = hptextField_2.getText();
				m.setMemhp(hp + hp2 + hp3);
				switch (chks.dupChkHp(m)) {
				case 1:
					msg = "회원 가입 된  핸드폰 번호 입니다.";
					hpTextField_1.setText(null);
					hptextField_2.setText(null);
					hpTextField_1.requestFocus();
					break;
				case 2:
					msg = "핸드폰 번호를 입력하세요.";
					hpTextField_1.requestFocus();
					break;
				case 3:
					msg = "핸드폰 번호를 정확히 입력하세요";
					hpTextField_1.requestFocus();
					break;

				default:
					msg = "핸드폰번호가 입력되었습니다.";
					emailTextField.requestFocus();
					isHp = true;
					break;
				}
				JOptionPane.showMessageDialog(null, msg);
				break;
			case "emailchk": // 이메일 검사
				email1 = (String) emailCbx.getSelectedItem();
				if (email1.equals("직접입력")) {
					m.setMememail(emailTextField.getText());
					switch (chks.dupChkEmail(m)) {
					case 1:
						msg = "회원 가입 된  이메일입니다.";
						emailTextField.setText(null);
						emailTextField.requestFocus();
						break;
					case 2:
						msg = "이메일을 입력해주세요";
						emailTextField.requestFocus();
						break;
					default:
						msg = "이메일이 입력되었습니다.";
						yearCbx.requestFocus();
						isEm = true;
						break;
					}
					JOptionPane.showMessageDialog(null, msg);
					break;
				} else {
					m.setMememail(emailTextField.getText() + "@" + email1);
					switch (chks.dupChkEmail(m)) {
					case 1:
						msg = "회원 가입 된  이메일입니다.";
						emailTextField.setText(null);
						emailTextField.requestFocus();
						break;
					case 2:
						msg = "이메일을 입력해주세요";
						emailTextField.requestFocus();
						break;
					default:
						msg = "이메일이 입력되었습니다.";
						yearCbx.requestFocus();
						isEm = true;
						break;
					}
					JOptionPane.showMessageDialog(null, msg);
				}
				break;

			case "OK": // 가입
				hp = (String) hpCbx.getSelectedItem();
				hp2 = hpTextField_1.getText();
				hp3 = hptextField_2.getText();
				String email = "";
				email1 = (String) emailCbx.getSelectedItem();
				String genre = (String) genreCbx.getSelectedItem();
				String birth = (String) yearCbx.getSelectedItem()
						+ (String) monthCbx.getSelectedItem()
						+ (String) dayCbx.getSelectedItem();
				if (email1.equals("직접입력")) {
					email = emailTextField.getText();
				} else {
					email = emailTextField.getText() + "@" + email1;
				}

				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < birth.length(); i++) {
					if (Character.isDigit(birth.charAt(i))) {
						sb.append(birth.charAt(i));
					}
				}
//				m.setMemid(idTextField.getText()); /////////////////////// 회원 정보 수정
				MemberLoginService mls = new MemberLoginService();////////////////////// 회원 정보 수정
				
				m.setMemid(m.getMemid());////////////////////// 회원 정보 수정
				m.setMempwd(new String(pwdField.getPassword()));
				m.setMemname(nameTextField.getText());
				m.setMemhp(hp + hp2 + hp3);
				m.setMememail(email);
				m.setMemgenre(genre);
				m.setMembirth(sb.toString());
				if ( m.getMemname().length() == 0///////////////////////회원정보수정
						|| m.getMempwd().length() == 0 || hp2.length() == 0||hp3.length() == 0 ) {
					JOptionPane.showMessageDialog(null, "정보를 전부 입력해주세요");
				}else if(isHp && isEm){
					ms.createMember(m);
					setVisible(false);
					dispose();
				}else{
					JOptionPane.showMessageDialog(null, "중복 확인 부탁드립니다 제발 플리즈~");
				}
				
				break;
			case "Cancel": // 취소
				setVisible(false);
				dispose();
				break;
				
			}
		}
		
		
	}

}
